package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.AttendanceMapper;
import com.itheima.mapper.WeeklyReportMapper;
import com.itheima.pojo.Attendance;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.User;
import com.itheima.pojo.WeeklyReport;

import com.itheima.service.WeeklReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.DayOfWeek;
import java.util.List;

@Service
public class WeeklyReportServiceImpl extends ServiceImpl<WeeklyReportMapper, WeeklyReport> implements WeeklReportService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private WeeklyReportMapper weeklyReportMapper;

//    @Override
//    public void generateWeeklyReport(Integer memberId, LocalDate weekStart, LocalDate weekEnd) {
//        List<Attendance> attendanceRecords = attendanceMapper.selectByMemberIdAndDateBetween(memberId, weekStart, weekEnd);
//
//        int totalDays = 0, absentDays = 0, lateDays = 0, earlyDays = 0;
//
//        for (Attendance record : attendanceRecords) {
//            if (record.getStatus().equals("正常")) {
//                totalDays++;
//            } else if (record.getStatus().equals("缺勤")) {
//                absentDays++;
//            } else if (record.getStatus().equals("迟到")) {
//                lateDays++;
//            } else if (record.getStatus().equals("早退")) {
//                earlyDays++;
//            }
//        }
//
//        WeeklyReport report = new WeeklyReport();
//        report.setMemberId(memberId);
//        report.setWeekStart(weekStart);
//        report.setWeekEnd(weekEnd);
//        report.setTotalDays(totalDays);
//        report.setAbsentDays(absentDays);
//        report.setLateDays(lateDays);
//        report.setEarlyDays(earlyDays);
//
//        weeklyReportMapper.insert(report);
//    }
//
//    @Override
//    public void generateReportsForAllStudents() {
//        LocalDate today = LocalDate.now();
//        LocalDate startOfWeek = today.with(DayOfWeek.MONDAY);  // 本周一
//        LocalDate endOfWeek = startOfWeek.plusDays(6);         // 本周日
//
//        // 获取所有学生ID并生成周报
//        List<Integer> memberIds = getAllMemberIds();
//        for (Integer memberId : memberIds) {
//            generateWeeklyReport(memberId, startOfWeek, endOfWeek);
//        }
//    }
//
//    private List<Integer> getAllMemberIds() {
//        // 假设有一个获取所有学生ID的列表
//        return List.of(1, 2, 3);  // 示例数据
//    }

//    @Override
//    public PageBean<WeeklyReport> getWeeklyReportsForMember(Integer pageNum, Integer pageSize, Integer memberId) {
//        // 1. 创建 PageBean 对象
//        PageBean<WeeklyReport> pb = new PageBean<>();
//
//        // 2. 开启分页查询 PageHelper
//        PageHelper.startPage(pageNum, pageSize);
//
//        // 查询条件封装
//        QueryWrapper<WeeklyReport> queryWrapper = new QueryWrapper<>();
//        if (memberId != null) {
//            queryWrapper.eq("member_id", memberId);
//        }
//
//        // 查询数据
//        List<WeeklyReport> reports = weeklyReportMapper.selectList(queryWrapper);
//
//        // Page 中提供了方法可以获取 PageHelper 分页查询后的总记录条数和当前页数据
//        Page<WeeklyReport> page = (Page<WeeklyReport>) reports;
//
//        // 将数据填充到 PageBean 对象中
//        pb.setTotal(page.getTotal());
//        pb.setItems(page.getResult());
//        return pb;
//    }

    @Override
    public PageBean<WeeklyReport> getWeeklyReportsForMember(Integer pageNum, Integer pageSize) {
        QueryWrapper<WeeklyReport> queryWrapper = new QueryWrapper<>();
        // 查询指定学生的所有周报记录
        PageBean<WeeklyReport> pb = new PageBean<>();

        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 使用自定义 SQL 查询，联接 User 表来获取 nickname
        List<WeeklyReport> as = weeklyReportMapper.selectAttendanceWithNickname();

        // 转换分页
        Page<WeeklyReport> page = (Page<WeeklyReport>) as;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());

        return pb;
    }


}
